<template>
    <div class="cart">
        <div class="sidebar">
            <div v-for="(item,i) in columns" :key="i" :class="Flag == i?'active':''" @click="Flag=i ">{{item.name}}</div>
        </div>
        <div class="merchandise">
            <ul>
                <li v-for="i in 5" :key="i">
                    <div class="left">
                        <img src="" alt="">
                        <div class="describe">
                            <p>雀巢咖啡</p>
                            <p>￥13.00</p>
                        </div>
                    </div>
                    <div class="number">
                        <div class="subnumber" v-if="cart.has('张三')" @click="removeCart()">-</div>
                        <span v-if="cart.has('张三') ">{{cart.get("张三")}}</span>
                        <div class="numberadd" @click="addToCart()">+</div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            cart: "",
            Flag: "",
            columns: [{ id: "1", name: "一号柜" }, { id: "2", name: "二号柜" }, { id: "3", name: "三号柜" }],
        }
    },
    methods: {
        addToCart() {
            this.cart.set('张三', 2);
            console.log([...this.cart])
        },
        removeCart() {
            if (this.cart.get("张三") - 1 == 0) {
                this.cart.remove("张三")
            } else {
                this.cart.set("张三", 11)
            }

        },
        Map() {
            this.keys = new Array();
            this.values = new Array();
            //添加键值对
            this.set = function(key, value) {
                if (this.values[key] == null) {//如键不存在则身【键】数组添加键名
                    this.keys.push(value);
                }
                this.values[key] = value;//给键赋值
            };
            //获取键对应的值
            this.get = function(key) {
                return this.values[key];
            };
            //去除键值，(去除键数据中的键名及对应的值)
            this.remove = function(key) {
                this.keys.remove(key);
                this.values[key] = null;
            };
            //判断键值元素是否为空
            this.isEmpty = function() {
                return this.keys.length == 0;
            };
            //获取键值元素大小
            this.size = function() {
                return this.keys.length;
            };
        }
    },
    beforeDestroy() {
        localStorage.setItem("shopCart", this.cart)
    },
    mounted() {
        this.cart = new Map();
        this.cart.set("张三", 11)
    },
}
</script>

<style scoped>
.cart {
    width: 100%;
    height: 100%;
    display: flex;
}

.sidebar {
    width: 24.5%;
    height: 100%;
    background: #f8f8f8;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    padding-right: 10px;
    overflow: auto;
    box-sizing: border-box;
    font-size: 14px;
    color: #a4a4a4;
}

.active {
    color: #222222;
}

.sidebar::-webkit-scrollbar {
    display: none;
}

.sidebar div {
    padding-top: 10px;
    line-height: 35px;
    text-align: center;
    height: 35px;
    border-bottom: 1px solid #d9d9d9;
}





/* .home .sidebar div:nth-last-of-type(1){
  border: 0;
} */


/* 商品分类 */

.merchandise {
    width: 75.5%;
    height: 100%;
    padding-left: 18px;
    padding-right: 16px;
}

.merchandise ul li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 7px;
    padding-bottom: 7px;
}

.merchandise .left {
    display: flex;
    align-items: center;
    font-size: 13px;
}

.describe p:nth-of-type(1) {
    color: #303030;
}

.describe p:nth-of-type(2) {
    color: #f32222;
}

.number {
    display: flex;
}

.number div {
    width: 18px;
    height: 18px;
    border-radius: 9px;
    box-sizing: border-box;
    text-align: center;
    line-height: 18px;
    font-size: 16px;
    background: #2fdd8a;
    color: #fff;
}

.number .subnumber {
    border: 1px solid #ccc;
    background: rgb(255, 255, 255);
    color: #ccc;
    line-height: 12px;
    font-size: 22px;
}

.number span {
    width: 30px;
}
</style>
